import { defineStore } from 'pinia';
import router from "@/router";
import {notification} from "ant-design-vue";

export const useUserStore = defineStore({
  id: 'user',
  state: () => ({
    token: '',
    userinfo: {}
  }),
  getters: {
    getUserinfo(state) {
      return state.userinfo;
    },
    accessToken(state) {
      return state.token;
    },
  },
  actions: {
    // 更新状态
    setToken(data) {
      this.token = data;
    },

    dispatchLogin(params) {
      // let loginParams = {...params};
      // return new Promise((resolve, reject) => {
      //   login(loginParams).then(data => {
      //     this.userinfo = data;
      //     this.token = data.access_token;
      //     resolve(data)
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    dispatchLogout(showMsg = false) {
      this.userinfo = {};
      this.token = '';
      router.push({
        path: '/login'
      }).then(() => {
        if(showMsg) {
          notification.success({
            message: '提示',
            description: `退出登录成功`
          })
        }
      })
    }
  },
  //持久化储存
  persist: true
})